<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="param">
                <el-form-item>
                    <el-input v-model="param.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="param.state" placeholder="请选择状态" clearable>
                        <el-option label="通过审核" value="1"></el-option>
                        <el-option label="待审核" value="0"></el-option>
                        <el-option label="驳回" value="-1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="searchKeyword">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="empList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="店铺名称" width="140" >
            </el-table-column>
            <el-table-column prop="registerTime" label="入驻时间" width="100" >
            </el-table-column>
            <el-table-column prop="tel" label="电话" width="180" >
            </el-table-column>
            <el-table-column prop="logo" label="图标" min-width="130" >
            </el-table-column>
            <el-table-column prop="state" label="状态" width="150" :formatter="formatState">
            </el-table-column>
            <el-table-column prop="address" label="地址" min-width="200" >
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template scope="scope">
                    <el-button type="warning" size="small" @click="check(scope.$index, scope.row)" :disabled="scope.row.state == 1">审核</el-button>
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="param.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--审核界面-->
        <el-dialog title="审核" :visible.sync="checkFormVisible" :close-on-click-modal="false">
            <el-form :model="checkForm" label-width="80px" >
                <el-form-item label="店铺名称" prop="name">
                    <el-input v-model="checkForm.name" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="入驻时间" prop="name">
                    <el-input v-model="checkForm.registerTime" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="name">
                    <el-input v-model="checkForm.tel" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="店铺图片">
                    <el-input v-model="checkForm.logo" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="checkForm.address" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="驳回原因">
                    <el-input type="textarea" v-model="checkForm.reason"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button  @click.native="addFormVisible = false">取消</el-button>
                <el-button type="success small" @click.native="pass" :loading="passLoading">通过</el-button>
                <el-button type="danger small" @click.native="respect" :loading="respectLoading">驳回</el-button>
            </div>
        </el-dialog>
        <!--新增\编辑界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="昵称" prop="username">
                    <el-input v-model="addForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="name">
                    <el-input v-model="addForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="name">
                    <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input-number v-model="addForm.age" :min="0" :max="200"></el-input-number>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">在职</el-radio>
                        <el-radio class="radio" :label="0">离职</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    export default {
        name: "emp",
        data(){
            return {
                passLoading:false,
                respectLoading:false,
                total:0,
                param:{
                    currentPage:1,
                    pageSize:5,
                    keyword:''
                },
                // 审核弹框
                checkFormVisible:false,
                // 列表加载
                listLoading:false,
                // 列表展示数据
                empList:{},
                sels:[],
                // 模态框显示
                addFormVisible:false,
                //模态框数据
                addForm:{
                    username:'',
                    email:'',
                    phone:'',
                    age:'',
                    state:1
                },
                // 模态框规则
                addFormRules:{
                    username: [
                        { required: true, message: '请输入昵称', trigger: 'blur' }
                    ],
                },
                // 提交按钮加载动画
                addLoading:false,
                checkForm:{
                    reason:''
                }
            }
        },
        methods:{
            check(index,row){
                this.checkFormVisible=true;
                console.log(row)
                this.checkForm=Object.assign({},row);
            },
            //驳回
            respect(){
                if(!this.checkForm.reason){
                    this.$message.error('驳回原因不能为空');
                    return;
                }
                // console.log(this.checkForm);
                this.respectLoading=true;

                this.$message({
                    message: 'loading....请耐心等待',
                    type: 'success'
                });
                this.$http.put("/shop/respect",this.checkForm).then(res=>{
                    this.respectLoading=false;
                    this.checkFormVisible=false;
                    if(res.data.success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: '网络异常',
                            type: 'warning'
                        });
                    }
                    this.loadList();
                })
            },
            // 审核通过
            pass(){
                this.passLoading=true;
                this.$message({
                    message: 'loading....请耐心等待',
                    type: 'success'
                });
                this.$http.put("/shop/pass",this.checkForm).then(res=>{
                    this.passLoading=false;
                    this.checkFormVisible=false;
                    if(res.data.success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: '网络异常',
                            type: 'warning'
                        });
                    }
                    this.loadList();
                })
            },
            // 高级查询
            searchKeyword(){
                this.param.currentPage=1;
                this.loadList();
            },
            // 新增按钮弹框
            handleAdd(){
                // this.addForm={
                //     username:'',
                //         email:'',
                //         phone:'',
                //         age:'',
                //         state:1
                // };
                // this.addFormVisible = true;
            },
            //每项前面的勾选
            selsChange(val){
                this.sels = val;
            },
            formatState(row){
                return row.state == 1 ? '通过审核' : row.state == 0 ? '待审核' : '驳回';
            },
            // 编辑按钮
            handleEdit(index,row){
                // this.addFormVisible=true;
                // this.addForm=Object.assign({},row);
            },
            // 删除按钮
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/shop/" + row.id).then(res => {
                        this.loadList();
                        if (res.data.success) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })
            },
            // 批量删除
            batchRemove(){
                // 拿到勾选的id数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/shop",ids).then(res=>{
                        this.loadList();
                        if (res.data.success) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })

            },
            // 当页码改变的时候
            handleCurrentChange(val){
                this.param.currentPage = val;
                this.loadList();
            },
            //模态框提交
            addSubmit(){
                this.$http.put("/shop",this.addForm).then(res=>{
                    this.addFormVisible=false;
                    this.loadList();
                    if(res.data.success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else{
                        this.$message({
                            message: '网络异常',
                            type: 'warning'
                        });
                    }
                })
            },
            // 加载数据
            loadList(){
                this.$http.post("/shop",this.param).then(res=>{
                    this.empList = res.data.data;
                    this.total=res.data.total;
                })
            }
        },
        mounted(){
            this.loadList();
        }
    }
</script>

<style scoped>

</style>